/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package blogic;

import bentity.*;
import dao.daoAlmacen;
import dao.daoHelper;
import dao.daoRack;
import java.awt.Point;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.ComboBoxEditor;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;

/**
 *
 * @author renzo
 */
public class blHelper {

    private daoAlmacen daoalm = new daoAlmacen();

    public DefaultComboBoxModel ComboBoxItem(ArrayList<beCombo> bec)
    {
        Object[] items=new Object[bec.size()];

        for (int i = 0; i < bec.size(); i++) {
            items[i]=new beItem(bec.get(i).getId_dato(), bec.get(i).getNom_dato()) ;
        }
        DefaultComboBoxModel mod=new DefaultComboBoxModel(items);

        return mod;

    }

    public DefaultListModel ListItem(ArrayList<beCombo> bec)
    {
        DefaultListModel mod=new DefaultListModel();

        for (int i = 0; i < bec.size(); i++) {
            mod.addElement(new beItem(bec.get(i).getId_dato(), bec.get(i).getNom_dato())) ;
        }

        return mod;
    }

    public ArrayList<beCombo> llenaCombo(String tipoCombo) throws SQLException{
        return ((new daoHelper()).DatosCombo(tipoCombo));
    }

    public boolean caracteresSoloDigitos(String cadena){
        if (cadena == null)
            return false;
        if (cadena.isEmpty() || cadena.length() == 0)
            return false;
        for (int i = 0; i < cadena.length(); i++) {
            if (!Character.isDigit(cadena.charAt(i)))
                return false;
        }
        return true;
    }

    public int calcularNumeroColumnas(int racksPorFila, int tamanhoRack) {
        return racksPorFila*tamanhoRack+racksPorFila+3;
    }

    public int calcularNumeroFilas(int racksPorCol) {
        return (racksPorCol/2)*3+3;
    }

    public char[][] obtenerMatrizAlmacen(int numCol, int numFil, int tamanhoRack) {
        char [][]matriz = new char[numCol+1][numFil+1];
        for(int i=0; i<numFil; i++){
            for(int j=0; j<numCol; j++){
                if (i==0 || i==(numFil-1))
                    matriz[j][i] = '*';
                else {
                    if ((i==(numFil-2)) && (j>0) && (j<(numCol-1)))
                        matriz[j][i] = ' ';
                    else {
                        if ((j==0 || j==(numCol-1)))
                            matriz[j][i] = '*';
                        else {
                            if ((j % (tamanhoRack+1)) == 1)
                                matriz[j][i] = ' ';
                            else{
                                if (((i-1) % 3) == 0)
                                    matriz[j][i] = ' ';
                                else
                                    matriz[j][i] = '*';
                            }
                        }
                    }
                }
            }
        }
        return matriz;
    }

    public  Point obtenerXYdeCoordenada(beCoordenada becor) throws SQLException{
        //NO BORREN
        //fila=y-((y-1)/3)-1
        //nroubicacion=x-(x/8)-1
        //nroubicaciondentrodelrack=nroubicacion mod 7
        //nrorack=nroubicacion/7
        Point p = new Point();

        p.x=becor.getRack()*8-6+becor.getUbicacion()-1;
        if ((becor.getFila() % 2) == 1)
            p.y = ((becor.getFila()+1)/2)*3-1;
        if ((becor.getFila() % 2) == 0)
            p.y = (becor.getFila()/2)*3;
        return p;
    }

    public boolean cadenaSoloDouble(String cadena){
        boolean hayPuntoDecimal=false;

        if (cadena == null)
            return false;
        if (cadena.isEmpty() || cadena.length() == 0)
            return false;
        for (int i = 0; i < cadena.length(); i++) {
            
            if (Character.isDigit(cadena.charAt(i))) continue;
            else if(cadena.charAt(i)=='.' && !hayPuntoDecimal) {
                hayPuntoDecimal=true;
                if(i==(cadena.length()-1)) return false;
                if(i==0) return false;
                continue;
            }
            else return false;
        }

        return true;
    }

    public boolean esDouble(String str){
        try{
            double num = Double.parseDouble(str);
            return true;
        }
        catch (Exception e){
            return false;
        }
    }

    public Date dameFechaHoy(){
        Calendar cal = Calendar.getInstance();
        return (new Date(cal.getTime().getTime()));
    }

    public beAlmacen obtenerDatosAlmacen() throws SQLException{
        daoRack daopepe = new daoRack();
        int ubics=daopepe.obtenerUbsXRack();
        int pisos= daopepe.obtenerPisosXRack();
        beAlmacen beal = new beAlmacen();
        beal=daoalm.obtenerAlmacenParaDespacho();
        beal.setUbsxrack(ubics);
        beal.setPisos(pisos);
        return beal;
    }

    public char[][] llenarMatrizEntradas(ArrayList<beEntrada> entradas, int numCol, int numFil, int tamanhoRack){
        char [][] eMatriz = this.obtenerMatrizAlmacen(numCol, numFil, tamanhoRack);
        for(int i=0;i<entradas.size();i++)
            eMatriz[((beEntrada)entradas.get(i)).getPos_x()][((beEntrada)entradas.get(i)).getPos_y()]='E';

        return eMatriz;
    }

    public int dameIdEstado(String nombreDato,String nombreCombo) throws SQLException{
        return (new daoHelper().dameIdEstado(nombreDato, nombreCombo));

    }

    public boolean cadenaMaximoCuatroDecimales(String cadena) {
        int indice=-1;

        if(!cadenaSoloDouble(cadena)) return false;

        for(int i=0;i<cadena.length();i++){
            if(cadena.charAt(i)=='.') {
                indice=i;
                break;
            }
        }

        if (indice==-1) return true;

        for(int i=indice+1;i<cadena.length();i++){
            if(i>4) return false;
        }

        return true;


    }

    public boolean cadenaContienePunto(String cadena){
        for(int i=0;i<cadena.length();i++){
            if(cadena.charAt(i)=='.') {
                return true;
            }
        }
        return false;
    }

    public String dameNombreEstado(int id,String nombreCombo) throws Exception{
        return (new daoHelper().dameNombreEstado(id, nombreCombo));
    }

    public static Date setTimeToMidnight(Date date) {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime( date );
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return (new Date(calendar.getTime().getTime()));
    }

}


